package com.iknow.xmpp.service;

import android.content.Context;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import com.iknow.IKnow;
import com.iknow.db.IKnowDataBase;
import com.iknow.util.Loger;
import com.iknow.xmpp.service.aidl.IBeemRosterListener;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IRoster.Stub;
import com.iknow.xmpp.smack.avatar.AvatarListener;
import com.iknow.xmpp.smack.avatar.AvatarManager;
import com.iknow.xmpp.smack.avatar.AvatarMetadataExtension.Info;
import com.iknow.xmpp.util.Status;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.util.StringUtils;

public class RosterAdapter extends IRoster.Stub
{
  private static final String TAG = "RosterAdapter";
  private final Roster mAdaptee;
  private Map<String, String> mAvatarIdmap;
  private AvatarManager mAvatarManager;
  private final ComparatorContactListByStatus<Contact> mComparator;
  private XMPPConnection mConnection;
  private final Map<Integer, String> mDefaultStatusMessages;
  private final RemoteCallbackList<IBeemRosterListener> mRemoteRosListeners = new RemoteCallbackList();
  private final RosterListenerAdapter mRosterListener;

  public RosterAdapter(Roster paramRoster, Context paramContext)
  {
    this.mRosterListener = new RosterListenerAdapter(this);
    this.mAvatarIdmap = new HashMap();
    this.mComparator = new ComparatorContactListByStatus(this, null);
    this.mAdaptee = paramRoster;
    paramRoster.addRosterListener(this.mRosterListener);
    this.mDefaultStatusMessages = createDefaultStatusMessagesMap(paramContext);
  }

  public RosterAdapter(Roster paramRoster, XMPPConnection paramXMPPConnection, IKnowUser paramIKnowUser, Context paramContext, AvatarManager paramAvatarManager)
  {
    this.mRosterListener = new RosterListenerAdapter(this);
    this.mAvatarIdmap = new HashMap();
    this.mComparator = new ComparatorContactListByStatus(this, null);
    this.mAdaptee = paramRoster;
    paramRoster.addRosterListener(this.mRosterListener);
    this.mDefaultStatusMessages = createDefaultStatusMessagesMap(paramContext);
    this.mAvatarManager = paramAvatarManager;
    if (this.mAvatarManager != null)
      this.mAvatarManager.addAvatarListener(new AvatarEventListener(this));
    this.mConnection = paramXMPPConnection;
    updateLocalDatabase();
  }

  private Map<Integer, String> createDefaultStatusMessagesMap(Context paramContext)
  {
    HashMap localHashMap = new HashMap();
    localHashMap.put(Integer.valueOf(500), paramContext.getString(2131361954));
    localHashMap.put(Integer.valueOf(600), paramContext.getString(2131361955));
    localHashMap.put(Integer.valueOf(300), paramContext.getString(2131361957));
    localHashMap.put(Integer.valueOf(400), paramContext.getString(2131361956));
    localHashMap.put(Integer.valueOf(100), paramContext.getString(2131361959));
    localHashMap.put(Integer.valueOf(200), paramContext.getString(2131361958));
    return localHashMap;
  }

  private Contact getContactFromRosterEntry(RosterEntry paramRosterEntry)
  {
    Iterator localIterator;
    String str = paramRosterEntry.getUser();
    Contact localContact = new Contact(str);
    Presence localPresence1 = this.mAdaptee.getPresence(str);
    if ((localPresence1.getStatus() == null) || ("".equals(localPresence1.getStatus())))
      localPresence1.setStatus((String)this.mDefaultStatusMessages.get(Integer.valueOf(Status.getStatusFromPresence(localPresence1))));
    localContact.setStatus(localPresence1);
    try
    {
      if (paramRosterEntry.getGroups() != null)
        localContact.setGroups(paramRosterEntry.getGroups());
      localIterator = this.mAdaptee.getPresences(str);
      if (!(localIterator.hasNext()))
      {
        localContact.setName(paramRosterEntry.getName());
        localContact.setAvatarId((String)this.mAvatarIdmap.get(str));
        return localContact;
      }
    }
    catch (NullPointerException localNullPointerException)
    {
      Presence localPresence2;
      do
      {
        while (true)
          Loger.d("RosterAdapter", "Group list not ready", localNullPointerException);
        localPresence2 = (Presence)localIterator.next();
      }
      while (localPresence2.getType().equals(Presence.Type.unavailable));
      localContact.addRes(StringUtils.parseResource(localPresence2.getFrom()));
    }
  }

  private int getItemType(RosterEntry paramRosterEntry)
  {
    int i = 0;
    if (paramRosterEntry.getType() == RosterPacket.ItemType.to)
      i = 1;
    while (true)
    {
      do
        while (true)
        {
          return i;
          if (paramRosterEntry.getType() != RosterPacket.ItemType.from)
            break;
          i = 2;
        }
      while (paramRosterEntry.getType() != RosterPacket.ItemType.both);
      i = 3;
    }
  }

  private void updateLocalDatabase()
  {
    IKnow.mIKnowDataBase.deleteAllRosterItem();
    Iterator localIterator = this.mAdaptee.getEntries().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      RosterEntry localRosterEntry = (RosterEntry)localIterator.next();
      IKnow.mIKnowDataBase.addRosterItem(localRosterEntry.getUser(), localRosterEntry.getName(), getItemType(localRosterEntry), Status.getStatusFromPresence(this.mAdaptee.getPresence(localRosterEntry.getUser())));
    }
  }

  public Contact addContact(String paramString1, String paramString2, String[] paramArrayOfString)
    throws RemoteException
  {
    Contact localContact;
    try
    {
      this.mAdaptee.createEntry(paramString1, paramString2, paramArrayOfString);
      RosterEntry localRosterEntry = this.mAdaptee.getEntry(paramString1);
      localContact = getContactFromRosterEntry(localRosterEntry);
      return localContact;
    }
    catch (XMPPException localXMPPException)
    {
      Loger.e("RosterAdapter", "Error while adding new contact", localXMPPException);
      localContact = null;
    }
  }

  public void addContactToGroup(String paramString1, String paramString2)
    throws RemoteException
  {
    createGroup(paramString1);
    RosterGroup localRosterGroup = this.mAdaptee.getGroup(paramString1);
    try
    {
      localRosterGroup.addEntry(this.mAdaptee.getEntry(paramString2));
      return;
    }
    catch (XMPPException localXMPPException)
    {
      localXMPPException.printStackTrace();
    }
  }

  public void addRosterListener(IBeemRosterListener paramIBeemRosterListener)
    throws RemoteException
  {
    if (paramIBeemRosterListener != null)
      this.mRemoteRosListeners.register(paramIBeemRosterListener);
  }

  public boolean cancelFollow(String paramString)
    throws RemoteException
  {
    int i;
    Presence localPresence = new Presence(Presence.Type.unsubscribe);
    localPresence.setTo(paramString);
    try
    {
      this.mConnection.sendPacket(localPresence);
      i = 1;
      return i;
    }
    catch (IllegalStateException localIllegalStateException)
    {
      Loger.e("RosterAdapter", "cancelFollow error:" + localIllegalStateException.getMessage());
      i = 0;
    }
  }

  public void createGroup(String paramString)
    throws RemoteException
  {
    if (this.mAdaptee.getGroup(paramString) == null)
      this.mAdaptee.createGroup(paramString);
  }

  public void deleteContact(Contact paramContact)
    throws RemoteException
  {
    RosterEntry localRosterEntry;
    try
    {
      localRosterEntry = this.mAdaptee.getEntry(paramContact.getJID());
      this.mAdaptee.removeEntry(localRosterEntry);
      IKnow.mIKnowDataBase.deleteFriend(StringUtils.parseName(paramContact.getJID()));
      return;
    }
    catch (XMPPException localXMPPException)
    {
      localXMPPException.printStackTrace();
    }
  }

  public Contact followFriend(String paramString1, String paramString2)
    throws RemoteException
  {
    Contact localContact;
    IQ localIQ;
    RosterPacket localRosterPacket = new RosterPacket();
    localRosterPacket.setType(IQ.Type.SET);
    RosterPacket.Item localItem = new RosterPacket.Item(paramString1, paramString2);
    localItem.setItemType(RosterPacket.ItemType.to);
    localRosterPacket.addRosterItem(localItem);
    PacketCollector localPacketCollector = this.mConnection.createPacketCollector(new PacketIDFilter(localRosterPacket.getPacketID()));
    try
    {
      this.mConnection.sendPacket(localRosterPacket);
      localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
      localPacketCollector.cancel();
      if (localIQ == null)
      {
        Loger.e("RosterAdapter", "No response from the server.");
        Presence localPresence = new Presence(Presence.Type.subscribe);
        localPresence.setTo(paramString1);
        this.mConnection.sendPacket(localPresence);
        localContact = getContact(paramString1);
        return localContact;
      }
    }
    catch (IllegalStateException localIllegalStateException)
    {
      do
        while (true)
        {
          Loger.e("RosterAdapter", "cancelFollow error:" + localIllegalStateException.getMessage());
          localContact = null;
        }
      while (localIQ.getType() != IQ.Type.ERROR);
      Loger.e("RosterAdapter", localIQ.getError().getMessage());
    }
  }

  public Contact getContact(String paramString)
    throws RemoteException
  {
    Contact localContact;
    if (this.mAdaptee.contains(paramString))
      localContact = getContactFromRosterEntry(this.mAdaptee.getEntry(paramString));
    while (true)
    {
      return localContact;
      localContact = null;
    }
  }

  public List<Contact> getContactList()
    throws RemoteException
  {
    Collection localCollection = this.mAdaptee.getEntries();
    ArrayList localArrayList = new ArrayList(localCollection.size());
    Iterator localIterator = localCollection.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return localArrayList;
      localArrayList.add(getContactFromRosterEntry((RosterEntry)localIterator.next()));
    }
  }

  public List<String> getFriendsByStatus(int paramInt1, int paramInt2)
    throws RemoteException
  {
    Collection localCollection = this.mAdaptee.getEntries();
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = localCollection.iterator();
    while (true)
    {
      RosterEntry localRosterEntry;
      do
      {
        if (!(localIterator.hasNext()))
        {
          Collections.sort(localArrayList, this.mComparator);
          return null;
        }
        localRosterEntry = (RosterEntry)localIterator.next();
      }
      while (localRosterEntry.getType() != RosterPacket.ItemType.both);
      localArrayList.add(getContactFromRosterEntry(localRosterEntry));
    }
  }

  public List<String> getGroupsNames()
    throws RemoteException
  {
    Collection localCollection = this.mAdaptee.getGroups();
    ArrayList localArrayList = new ArrayList(localCollection.size());
    Iterator localIterator = localCollection.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return localArrayList;
      localArrayList.add(((RosterGroup)localIterator.next()).getName());
    }
  }

  public PresenceAdapter getPresence(String paramString)
    throws RemoteException
  {
    return new PresenceAdapter(this.mAdaptee.getPresence(paramString));
  }

  public void removeContactFromGroup(String paramString1, String paramString2)
    throws RemoteException
  {
    RosterGroup localRosterGroup = this.mAdaptee.getGroup(paramString1);
    try
    {
      localRosterGroup.removeEntry(this.mAdaptee.getEntry(paramString2));
      return;
    }
    catch (XMPPException localXMPPException)
    {
      localXMPPException.printStackTrace();
    }
  }

  public void removeRosterListener(IBeemRosterListener paramIBeemRosterListener)
    throws RemoteException
  {
    if (paramIBeemRosterListener != null)
      this.mRemoteRosListeners.unregister(paramIBeemRosterListener);
  }

  public void setContactName(String paramString1, String paramString2)
    throws RemoteException
  {
    this.mAdaptee.getEntry(paramString1).setName(paramString2);
  }

  private class AvatarEventListener
  implements AvatarListener
  {
    public void onAvatarChange(, String paramString2, List<AvatarMetadataExtension.Info> paramList)
    {
      String str = StringUtils.parseBareAddress(paramString1);
      if (paramString2 == null)
        RosterAdapter.access$4(this.this$0).remove(str);
      while (true)
      {
        do
          return;
        while (paramList.size() <= 0);
        RosterAdapter.access$4(this.this$0).put(str, paramString2);
      }
    }
  }

  private class ComparatorContactListByStatus<T>
  implements Comparator<T>
  {
    public int compare(, T paramT2)
    {
      int i;
      Contact localContact1 = (Contact)paramT1;
      Contact localContact2 = (Contact)paramT2;
      if (localContact1.getStatus() == localContact2.getStatus())
        i = 0;
      while (true)
      {
        while (true)
        {
          return i;
          if (localContact1.getStatus() <= localContact2.getStatus())
            break;
          i = -1;
        }
        i = 1;
      }
    }
  }

  private class RosterListenerAdapter
  implements RosterListener
  {
    public void entriesAdded()
    {
      int j;
      int i = RosterAdapter.access$0(this.this$0).beginBroadcast();
      ArrayList localArrayList = new ArrayList();
      localArrayList.addAll(paramCollection);
      Iterator localIterator = localArrayList.iterator();
      while (true)
      {
        if (!(localIterator.hasNext()))
        {
          j = 0;
          if (j < i)
            break;
          RosterAdapter.access$0(this.this$0).finishBroadcast();
          return;
        }
        String str = (String)localIterator.next();
        RosterEntry localRosterEntry = RosterAdapter.access$1(this.this$0).getEntry(str);
        IKnow.mIKnowDataBase.addRosterItem(localRosterEntry.getUser(), localRosterEntry.getName(), RosterAdapter.access$2(this.this$0, localRosterEntry), Status.getStatusFromPresence(RosterAdapter.access$1(this.this$0).getPresence(str)));
      }
      IBeemRosterListener localIBeemRosterListener = (IBeemRosterListener)RosterAdapter.access$0(this.this$0).getBroadcastItem(j);
      try
      {
        localIBeemRosterListener.onEntriesAdded(localArrayList);
        ++j;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
          Loger.w("RosterAdapter", "Error while adding roster entries", localRemoteException);
      }
    }

    public void entriesDeleted()
    {
      int j;
      int i = RosterAdapter.access$0(this.this$0).beginBroadcast();
      ArrayList localArrayList = new ArrayList();
      localArrayList.addAll(paramCollection);
      Iterator localIterator = localArrayList.iterator();
      while (true)
      {
        if (!(localIterator.hasNext()))
        {
          j = 0;
          if (j < i)
            break;
          RosterAdapter.access$0(this.this$0).finishBroadcast();
          return;
        }
        String str = (String)localIterator.next();
        IKnow.mIKnowDataBase.deleteRosterItem(str);
      }
      IBeemRosterListener localIBeemRosterListener = (IBeemRosterListener)RosterAdapter.access$0(this.this$0).getBroadcastItem(j);
      try
      {
        localIBeemRosterListener.onEntriesDeleted(localArrayList);
        ++j;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
          Loger.w("RosterAdapter", "Error while deleting roster entries", localRemoteException);
      }
    }

    public void entriesUpdated()
    {
      int j;
      int i = RosterAdapter.access$0(this.this$0).beginBroadcast();
      ArrayList localArrayList = new ArrayList();
      localArrayList.addAll(paramCollection);
      Iterator localIterator = localArrayList.iterator();
      while (true)
      {
        String str;
        RosterEntry localRosterEntry;
        do
        {
          do
          {
            if (!(localIterator.hasNext()))
            {
              j = 0;
              if (j < i)
                break label159;
              RosterAdapter.access$0(this.this$0).finishBroadcast();
              return;
            }
            str = (String)localIterator.next();
            localRosterEntry = RosterAdapter.access$1(this.this$0).getEntry(str);
          }
          while (localRosterEntry == null);
          if (localRosterEntry.getType() == RosterPacket.ItemType.both)
            IKnow.mIKnowDataBase.updateRosterItemSubscribeMode(str, 3);
          if (localRosterEntry.getType() == RosterPacket.ItemType.to)
            IKnow.mIKnowDataBase.updateRosterItemSubscribeMode(str, 1);
        }
        while (localRosterEntry.getType() != RosterPacket.ItemType.from);
        IKnow.mIKnowDataBase.updateRosterItemSubscribeMode(str, 2);
      }
      label159: IBeemRosterListener localIBeemRosterListener = (IBeemRosterListener)RosterAdapter.access$0(this.this$0).getBroadcastItem(j);
      try
      {
        localIBeemRosterListener.onEntriesUpdated(localArrayList);
        ++j;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
          Loger.w("RosterAdapter", "Error while updating roster entries", localRemoteException);
      }
    }

    public void presenceChanged()
    {
      int i = RosterAdapter.access$0(this.this$0).beginBroadcast();
      Loger.v("RosterAdapter", ">>> Presence changed for " + paramPresence.getFrom());
      int j = 0;
      if (j >= i)
      {
        RosterAdapter.access$0(this.this$0).finishBroadcast();
        return;
      }
      IBeemRosterListener localIBeemRosterListener = (IBeemRosterListener)RosterAdapter.access$0(this.this$0).getBroadcastItem(j);
      try
      {
        if ((paramPresence.getStatus() == null) || ("".equals(paramPresence.getStatus())))
          paramPresence.setStatus((String)RosterAdapter.access$3(this.this$0).get(Integer.valueOf(Status.getStatusFromPresence(paramPresence))));
        IKnow.mIKnowDataBase.updateRosterPresence(paramPresence.getFrom(), Status.getStatusFromPresence(paramPresence));
        localIBeemRosterListener.onPresenceChanged(new PresenceAdapter(paramPresence));
        ++j;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
          Loger.w("RosterAdapter", "Error while updating roster presence entries", localRemoteException);
      }
    }
  }
}